import { featureCollection, point } from '@turf/helpers';
import circle from '@turf/circle';
import gcoord from 'gcoord';

export const transformCollection = (data) => {
  return data.split(';').filter(item => item.trim()).map(item => {
    const r = item.split(',');
    const coordinates = gcoord.transform([parseFloat(r[1]), parseFloat(r[2])], gcoord.WGS84, gcoord.GCJ02);
    const properties = {
      value: parseFloat(r[0]),
      lngLat: coordinates,
    };
    return { coordinates, properties };
  });
}

export const transformFeatureCollection = (collection, { radius, type } = { radius: 2, type: 'circle' }) => {
  return featureCollection(collection.map(item => {
    switch(type) {
      case 'circle':
        return circle(item.coordinates, radius, { steps: 10, units: 'meters', properties: item.properties });
      default:
        return point(item.coordinates, item.properties);
    }
  }));
}
